package com.books.service.admin.impl;

import com.books.mapper.address.AddressMapper;
import com.books.mapper.admin.AdminMapper;
import com.books.mapper.admin.UserMapper;
import com.books.mapper.admin.VipNumToolMapper;
import com.books.mapper.back.BackMapper;
import com.books.mapper.book.BookMapper;
import com.books.mapper.book.BookTypeMapper;
import com.books.mapper.book.EbookMapper;
import com.books.mapper.common.*;
import com.books.mapper.order.BuyOrderMapper;
import com.books.mapper.order.OrderListMapper;
import com.books.model.entity.address.Receiving_address;
import com.books.model.entity.back.Back;
import com.books.model.entity.backcontroller.*;
import com.books.model.entity.book.BookType;
import com.books.model.entity.book.Books;
import com.books.model.entity.book.Ebook;
import com.books.model.entity.common.People;
import com.books.model.entity.order.BuyOrder;
import com.books.model.entity.order.OrderList;
import com.books.model.tool.Data;
import com.books.model.tool.MonthPriceTool;
import com.books.model.tool.VipNumTool;
import com.books.service.admin.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 饿熊的咆哮
 * @Date 2021/9/26 20:41
 * @Version 1.0
 */
@Service
@Transactional
public class AdminServiceImpl implements IAdminService {
    @Autowired(required = false)
    private AdminMapper adminMapper;

    @Autowired(required = false)
    private BookTypeMapper bookTypeMapper;

    @Autowired(required = false)
    private PeopleMapper peopleMapper;

    @Autowired(required = false)
    private VipMapper vipMapper;

    @Autowired(required = false)
    private AnnouncementMapper announcementMapper;

    @Autowired(required = false)
    private ChatMapper chatMapper;

    @Autowired(required = false)
    private VipNumToolMapper vipNumToolMapper;

    @Autowired(required = false)
    private StatusMapper statusMapper;

    @Autowired(required = false)
    private BookMapper bookMapper;

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private EbookMapper ebookMapper;

    @Autowired(required = false)
    private AddressMapper addressMapper;

    @Autowired(required = false)
    private BackMapper backMapper;

    @Autowired(required = false)
    private BuyOrderMapper buyOrderMapper;

    @Autowired(required = false)
    private OrderListMapper orderListMapper;



    @Override
    public List<Data> findVipNum() {
        List<VipNumTool> vipNumTools = vipNumToolMapper.findAll();
        List<Data> dataList = new ArrayList<>();
        for (VipNumTool tool : vipNumTools) {
            Data data = new Data();
            if (tool.getVipId()!=-1){
                data.setValue(tool.getVipNum());
                if (tool.getVipId()==0){
                    data.setName("非会员");
                }else {
                    data.setName(vipMapper.findByVipId(tool.getVipId()).getVipName());
                }
                dataList.add(data);
            }
        }
        return dataList;
    }

    @Override
    public List<MonthPriceTool> getMonthMoney() {
        List<MonthPriceTool> monthPriceTools = buyOrderMapper.getMonthMoney();
        return monthPriceTools;
    }

    @Override
    public List<Admin> findAll() {
        List<Admin> adminList = adminMapper.findAll();
        return adminList;
    }

    @Override
    public Admin findAdminByPeopleId(Integer peopleId) {
        Admin admin = adminMapper.findAdminByPeopleId(peopleId);
        return admin;
    }

    @Override
    public Admin findByAccount(String account) {
        Admin admin = adminMapper.findByAccount(account);
        return admin;
    }

    @Override
    public Admin findByAdminId(Integer adminId) {
        Admin admin = adminMapper.findByAdminId(adminId);
        return admin;
    }

    @Override
    public List<Admin> findByNickName(String nickName) {
        List<Admin> adminList = adminMapper.findByNickName(nickName);
        return adminList;
    }

    @Override
    public void updateByAccount(Admin admin, String account) {
        adminMapper.updateByAccount(admin,account);
    }

    @Override
    public boolean delByAdminId(Integer adminId) {
        adminMapper.delByAdminId(adminId);
        return true;
    }

    @Override
    public void addAdmin(Admin admin) {
        adminMapper.addAdmin(admin);
    }


    @Override
    public List<BookType> findAllBookCategory() {
        List<BookType> bookCategoryList = bookTypeMapper.findAll();
        return bookCategoryList;
    }

    @Override
    public BookType findByCategoryName(String categoryName) {
        BookType bookCategory = bookTypeMapper.findByCategoryName(categoryName);
        return bookCategory;
    }

    @Override
    public BookType findByBookTypeId(int typeId) {
        BookType bookCategory = bookTypeMapper.findById(typeId);
        return bookCategory;
    }

    @Override
    public List<People> findAllPeople() {
        List<People> peopleList = peopleMapper.findAll();
        return peopleList;
    }

    @Override
    public People findByPhone(String phone) {
        People people = peopleMapper.findByPhone(phone);
        return people;
    }

    @Override
    public People findByPeopleId(Integer peopleId) {
        People people = peopleMapper.findByPeopleId(peopleId);
        return people;
    }

    @Override
    public List<People> findByPeople(People people) {
        if (people.getPeopleId()!=null){
            People people1 = peopleMapper.findByPeopleId(people.getPeopleId());
            List<People> peopleList = new ArrayList<>();
            peopleList.add(people1);
            return peopleList;
        }else if (people.getIdNum()!=null && people.getIdNum().length()>0){
            People people1 = peopleMapper.findByIdNum(people.getIdNum());
            List<People> peopleList = new ArrayList<>();
            peopleList.add(people1);
            return peopleList;
        }else if (people.getName()!=null && people.getName().length()>0){
            List<People> peopleList = peopleMapper.findByName(people.getName());
            return peopleList;
        }else if (people.getPhone()!=null && people.getPhone().length()>0){
            People people1 = peopleMapper.findByPhone(people.getPhone());
            List<People> peopleList = new ArrayList<>();
            peopleList.add(people1);
            return peopleList;
        }else if (people.getEmail()!=null && people.getEmail().length()>0){
            People people1 = peopleMapper.findByEmail(people.getEmail());
            List<People> peopleList = new ArrayList<>();
            peopleList.add(people1);
            return peopleList;
        }else if (people.getQq()!=null && people.getQq().length()>0){
            People people1 = peopleMapper.findByQQ(people.getQq());
            List<People> peopleList = new ArrayList<>();
            peopleList.add(people1);
            return peopleList;
        }else if (people.getWx()!=null && people.getWx().length()>0){
            People people1 = peopleMapper.findByWX(people.getWx());
            List<People> peopleList = new ArrayList<>();
            peopleList.add(people1);
            return peopleList;
        }
        return null;
    }

    @Override
    public void addPeople(People people) {
        peopleMapper.addPeople(people);
    }

    @Override
    public void updateByPeopleId(People people, Integer peopleId) {
        peopleMapper.updatePeople(people,peopleId);
    }

    @Override
    public void delByPeopleId(Integer peopleId) {
        peopleMapper.delByPeopleId(peopleId);
    }

    @Override
    public List<Vip> findAllVip() {
        List<Vip> vipList = vipMapper.findAll();
        return vipList;
    }

    @Override
    public Vip findByVipId(Integer vipId) {
        Vip vip = vipMapper.findByVipId(vipId);
        return vip;
    }

    @Override
    public List<Vip> findByVipName(String vipName) {
        List<Vip> vips = vipMapper.findByVipName(vipName);
        return vips;
    }

    @Override
    public void addVip(Vip vip) {
        vipMapper.addVip(vip);
    }

    @Override
    public void updateVip(Vip vip, Integer vipId) {
        vipMapper.updateVip(vip,vipId);
    }

    @Override
    public List<Announcement> findAllAnnouncement() {
        List<Announcement> announcements = announcementMapper.findAll();
        return announcements;
    }

    @Override
    public List<Announcement> findByAnnouncement(Announcement announcement) {
        if (announcement.getAnnouncementId()!=null){
            Announcement announcement1 = announcementMapper.findByAnnouncementId(announcement.getAnnouncementId());
            List<Announcement> announcements = new ArrayList<>();
            announcements.add(announcement1);
            return announcements;
        }else if (announcement.getAnnouncementTitle()!=null && announcement.getAnnouncementTitle().length()>0){
            List<Announcement> announcements = announcementMapper.findByAnnouncementTitle(announcement.getAnnouncementTitle());
            return announcements;
        }else if (announcement.getAdminId()!=null){
            List<Announcement> announcements = announcementMapper.findByAdminId(announcement.getAdminId());
            return announcements;
        }else {
            return null;
        }

    }

    @Override
    public void addAnnouncement(Announcement announcement) {
        announcementMapper.addAnnouncement(announcement);
    }

    @Override
    public void delByAnnouncementId(Integer announcementId) {
        announcementMapper.delByAnnouncementId(announcementId);
    }

    @Override
    public List<Chat> findChatByTake(Integer takePeopleId) {
        List<Chat> chats = chatMapper.findByTake(takePeopleId);
        return chats;
    }

    @Override
    public List<Chat> findRead(Integer takePeopleId, boolean ifRead) {
        List<Chat> chats = chatMapper.findRead(takePeopleId,ifRead);
        return chats;
    }

    @Override
    public List<Chat> findHistoryChat(Integer adminPeopleId, Integer userPeopleId) {
        List<Chat> chats = chatMapper.findHistoryChat(adminPeopleId,userPeopleId);
        return chats;
    }

    @Override
    public void addChat(Chat chat) {
        chatMapper.addChat(chat);
    }

    @Override
    public void delChat(Integer peopleId) {
        chatMapper.delChat(peopleId);
    }

    @Override
    public void updateChat(boolean ifRead, Integer chatId) {
        chatMapper.updateChat(ifRead,chatId);
    }

    @Override
    public List<People> findUser() {
        List<People> user = peopleMapper.findUser();
        return user;
    }

    @Override
    public String findStatus(Integer statusId) {
        Status status = statusMapper.findStatusId(statusId);
        if (status!=null){
            return status.getStatus();
        }
        return null;
    }

    @Override
    public Integer findStatusId(String status) {
        Integer statusId = statusMapper.findByStatus(status).getStatusId();
        return statusId;
    }

    @Override
    public void updateVipIntegral(Double vipIntegral,Integer vipId,Integer peopleId) {
        peopleMapper.updateVipIntegral(vipIntegral,vipId,peopleId);
    }

    @Override
    public List<People> findUserByPeople(People people) {
        if (people.getName()!=null && people.getName().length()>0){
            List<People> peopleList = userMapper.findByName(people.getName());
            return peopleList;
        }else if (people.getPhone()!=null && people.getPhone().length()>0){
            People people1 = userMapper.findByPhone(people.getPhone());
            List<People> peopleList = new ArrayList<>();
            peopleList.add(people1);
            return peopleList;
        }
        return null;
    }

    @Override
    public List<Receiving_address> findAllAddress(int people_id) {
        List<Receiving_address> addresses = addressMapper.findAll(people_id);
        return addresses;
    }

    @Override
    public List<Books> findAllBook() {
        List<Books> books = bookMapper.findAll();
        return books;
    }

    @Override
    public Books findByBookId(Integer bookId) {
        Books books = bookMapper.getById(bookId);
        return books;
    }

    @Override
    public List<Books> findByBook(Books books) {
        if (books.getTitle()!=null && books.getTitle().length()>0){
            List<Books> booksList = bookMapper.findByBookName(books.getTitle());
            return booksList;
        }else if (books.getAuthor()!=null && books.getAuthor().length()>0){
            List<Books> booksList = bookMapper.findByAuthor(books.getAuthor());
            return booksList;
        }else if (books.getCategoryId()!=null){
            List<Books> booksList = bookMapper.findByBookType(books.getCategoryId());
            return booksList;
        }
        return null;
    }

    // @Override
    // public List<Books> findByBookName(String title) {
    //     List<Books> books = bookMapper.findByBookName(title);
    //     return books;
    // }
    //
    // @Override
    // public List<Books> findByAuthor(String author) {
    //     List<Books> books = bookMapper.findByAuthor(author);
    //     return books;
    // }
    //
    // @Override
    // public List<Books> findByBookType(String category) {
    //     Integer categoryId = bookTypeMapper.findByCategoryName(category).getCategoryId();
    //     List<Books> books = bookMapper.findByBookType(categoryId);
    //     return books;
    // }

    @Override
    public void delByBkId(Integer bkId) {
        bookMapper.delByBkId(bkId);
    }

    @Override
    public void updateBook(Integer bkId, Double price) {
        bookMapper.updateBook(bkId,price);
    }

    @Override
    public void updateBookStock(Integer bkId, Integer stock) {
        bookMapper.updateBookStock(bkId,stock);
    }

    @Override
    public void addBook(Books books) {
        bookMapper.addBook(books);
    }

    @Override
    public List<Ebook> findAllEBook() {
        List<Ebook> ebooks = ebookMapper.findAll();
        return ebooks;
    }

    @Override
    public Ebook findByEBookId(Integer EbookId) {
        Ebook ebook = ebookMapper.getById(EbookId);
        return ebook;
    }

    @Override
    public List<Ebook> findByEBook(Ebook ebook) {
        if (ebook.getTitle()!=null && ebook.getTitle().length()>0){
            List<Ebook> ebookList = ebookMapper.findByBookName(ebook.getTitle());
            return ebookList;
        }else if (ebook.getAuthor()!=null && ebook.getAuthor().length()>0){
            List<Ebook> ebookList = ebookMapper.findByAuthor(ebook.getAuthor());
            return ebookList;
        }else if (ebook.getCategoryId()!=null){
            List<Ebook> ebookList = ebookMapper.findByBookType(ebook.getCategoryId());
            return ebookList;
        }
        return null;
    }

    @Override
    public void delByEBkId(Integer ebkId) {
        ebookMapper.delByBkId(ebkId);
    }

    @Override
    public void updateEBook(Integer ebkId, Double price) {
        ebookMapper.updateBook(ebkId,price);
    }

    @Override
    public void addEBook(Ebook ebook) {
        ebookMapper.addBook(ebook);
    }

    @Override
    public List<BuyOrder> getAllOrders() {
        List<BuyOrder> buyOrders = buyOrderMapper.getAllOrders();
        return buyOrders;
    }

    @Override
    public List<BuyOrder> findBy(BuyOrder buyOrder) {
        if (buyOrder.getBuyId()!=null&&buyOrder.getBuyId().length()>0){
            List<BuyOrder> buyOrders = new ArrayList<>();
            BuyOrder buyOrder1 = buyOrderMapper.findOneOrder(buyOrder.getBuyId());
            buyOrders.add(buyOrder1);
            return buyOrders;
        }else if (buyOrder.getUserId()!=null){
            List<BuyOrder> buyOrders = buyOrderMapper.findAllOrder(buyOrder.getUserId());
            return buyOrders;
        }else {
            return null;
        }
    }

    @Override
    public List<OrderList> findAllList(String buyId) {
        List<OrderList> orderLists = orderListMapper.findAllList(buyId);
        return orderLists;
    }

    @Override
    public void updateBuyStatus(Integer buyStatus, String id) {
        orderListMapper.updateBuyStatus(buyStatus,id);
    }

    @Override
    public List<OrderList> getAllList() {
        List<OrderList> orderLists = orderListMapper.getAllList();
        return orderLists;
    }

    @Override
    public OrderList findList(String id) {
        OrderList orderList = orderListMapper.findList(id);
        return orderList;
    }

    @Override
    public List<Back> findAllBack() {
        List<Back> backs = backMapper.findAllBack();
        return backs;
    }

    @Override
    public void delByBackId(String orderListId) {
        backMapper.delByBackId(orderListId);
    }
}
